var delbtn = document.getElementById("del");
//删除按钮
var changead = document.getElementById("changead");
//标题内容
var setaddress = document.getElementById("set");
//设置默认值
function Addaddress($scope, $http) {
	var province, city, county, addressId;
	//如果本地存储有地址信息，则为地址编辑，将地址信息填写到编辑框
	if (localStorage.address) {
		var addressMe = eval('(' + readData(localStorage.address) + ')');
		//alert(localStorage.address);
		$scope.uname = addressMe.shipname;
		$scope.phone = addressMe.phone;
		if (addressMe.province) {
			province = addressMe.province
		} else {
			province = ''
		};
		if (addressMe.city) {
			city = addressMe.city
		} else {
			city = ''
		};
		if (addressMe.area) {
			county = addressMe.area
		} else {
			county = ''
		};
		$scope.city = province + city + county;
		$scope.address = addressMe.shipaddress;
		//添加是否为默认对象
		$scope.defaultAddr = addressMe.defaultAddr;
		addressId = addressMe.id;
		changead.innerText = "编辑地址";
		//控制标题内容
		if (addressMe.defaultAddr == "n") { //判断是否为默认地址控制按钮字体变化
			setaddress.innerText = "设为默认地址"
		} else {
			setaddress.innerText = "取消默认地址"
		}
	} else {
		$scope.defaultAddr = 'n';
		delbtn.style.display = "none";
		changead.innerText = "新增地址";
	}
	$scope.changeSet = function() {
			if ($scope.defaultAddr == "n" || typeof($scope.defaultAddr) == "undefined") {
				$scope.defaultAddr = "y";
				setaddress.innerText = "取消默认地址"
			} else {
				$scope.defaultAddr = "n";
				setaddress.innerText = "设为默认地址"
			}
		}
		//地址提交
	$scope.sub = function() {
			//判断是否为空
			if (!$scope.uname || !$scope.phone || !$scope.address || !$scope.city) {
				mui.toast("地址信息填写不完整,请完善后再提交")
				return;
			}
			reg = /^1[0-9]{10}$/;
			if (!reg.test($scope.phone)) {
				mui.toast('手机号码有误，请重新输入');
				return;
			}
			//判断是编辑还是新增
			plus.nativeUI.showWaiting("正在提交…")
			if (localStorage.address) {
				//编辑地址提交
				url = getUrl("39addressEdit");
				header = getHeader();
				datas = "id=" + addressId + "&accountId=" + readData(localStorage.account) + "&shipname=" + $scope.uname + "&shipaddress=" + $scope.address + "&province=" + province + "&city=" + city + "&area=" + county + "&phone=" + $scope.phone + "&defaultAddr=" + $scope.defaultAddr;
				$http({
					method: 'POST',
					url: url,
					headers: header,
					timeout: 10000,
					//cache: true,
					//params: params
					data: datas
				}).success(function(data, status, headers, config) {
					localStorage.removeItem("address");
					if (data.code == "0") {
						reloadAddresslist()
					}
					plus.nativeUI.closeWaiting()
				}).error(function(data, status, headers, config) {
					plus.nativeUI.closeWaiting()
					mui.toast("网络链接异常，请稍后重试(e)")
				});
			} else {
				//新增地址提交
				url = getUrl("24addressAdd");
				header = getHeader();
				datas = "accountId=" + readData(localStorage.account) + "&shipname=" + $scope.uname + "&shipaddress=" + $scope.address + "&province=" + province + "&city=" + city + "&area=" + county + "&phone=" + $scope.phone + "&defaultAddr=" + $scope.defaultAddr;
				$http({
					method: 'POST',
					url: url,
					headers: header,
					timeout: 10000,
					//cache: true,
					//params: params
					data: datas
				}).success(function(data, status, headers, config) {
					if (data.code == "0") {
						reloadAddresslist()
					}
					plus.nativeUI.closeWaiting()
				}).error(function(data, status, headers, config) {
					mui.toast("网络链接异常，请稍后重试(e)")
					plus.nativeUI.closeWaiting()
				});
			}
		}
		//删除地址
	$scope.deleteAddr = function() {
			urlDel = getUrl("40addressDel");
			headerDel = getHeader();
			datasDel = "id=" + addressId;
			plus.nativeUI.showWaiting("正在删除……")
			$http({
				method: 'POST',
				url: urlDel,
				headers: headerDel,
				timeout: 10000,
				//cache: true,
				//params: params
				data: datasDel
			}).success(function(data, status, headers, config) {
				localStorage.removeItem("address");
				if (data.code == "0") {
					reloadAddresslist()
				}
				plus.nativeUI.closeWaiting()
			}).error(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting()
				mui.toast("网络链接异常，请稍后重试(e)")

			});
		}
		//返回功能
	$scope.back = function() {
			localStorage.removeItem("address");
			mui.back()
		}
		//城市选择
	$scope.open = function() {
		var cityPicker3 = new mui.PopPicker({
			layer: 3
		});
		cityPicker3.setData(cityData3);
		cityPicker3.show(function(items) {
			//alert("你选择的城市是:" + (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text);
			//返回 false 可以阻止选择框的关闭
			//return false;
			province = (items[0] || {}).text;
			city = (items[1] || {}).text;
			county = (items[2] || {}).text;
			$scope.city = province + city + county;
			document.getElementById("city").value = province + city + county;
		});

	};
}

function reloadAddresslist() {
	var parent = plus.webview.currentWebview().opener()
	mui.fire(parent, "reload");
	setTimeout(function() {
		mui.back();
	}, 300)

}
mui.back = function() {
	delete localStorage.address;
	plus.webview.currentWebview().close();
}